package com.micromall.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Map;

/**
 * Created by DanteFung on 2017/6/21.
 */
public class JsonResult<T> {

    public static final State SUCCESS = new State("200","请求成功!");
    public static final State FAIL = new State("400","请求失败!");
    public static final State TOKEN_EXPIRED = new State("405","Token失效!");
    public static final State PARAMETER_EMPTY = new State("406","参数为空!");
    public static final State BUSSINESS_FAIL = new State("407","业务错误!程序自定义返回消息");

    private String status;

    private T data;

    private String msg;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty);
    }

    public String toJsonpString(String callback){
        StringBuffer sb = new StringBuffer();
        sb.append(StringUtils.isNotBlank(callback)?callback:"jsonp").append("(").append(this.toString()).append(")");
        return sb.toString();
    }

    public static <T> String toString(String status, String msg, T data){
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.setStatus(status);
        jsonResult.setMsg(msg);
        jsonResult.setData(data);
        return jsonResult.toJsonpString(null);
    }

    public static <T> String toJsonpString(String status, String msg, T data, String callback){
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.setStatus(status);
        jsonResult.setMsg(msg);
        jsonResult.setData(data);
        return jsonResult.toJsonpString(callback);
    }

    public static <T>  String toString(String status, String msg, T data, Map extKeyValue){
        String targetStr = null;
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.setStatus(status);
        jsonResult.setMsg(msg);
        jsonResult.setData(data);
        if(null != extKeyValue){
            JSONObject jsonObject = (JSONObject)JSONObject.toJSON(jsonResult);
            for(Object key : extKeyValue.keySet()){
                jsonObject.put(key+"",extKeyValue.get(key));
            }
            targetStr = "jsonp("+jsonObject.toString()+")";
        }else{
            targetStr = jsonResult.toJsonpString(null);
        }

        return targetStr;
    }

    public static class State{
        private String code;
        private String desc;

        public State(String code, String desc){
            this.code = code;
            this.desc = desc;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
